<?php


namespace App\Http\Controllers;


use App\Processes\TestProcess;
use co;
use Illuminate\Support\Facades\DB;
use Swoole\Coroutine;
use Swoole\Http\Server;
use Swoole\Process;
use function Co\run;

class XieCheng
{

        function test(){

                $channel = new \Swoole\Coroutine\Channel(3);
                $results = [];
                $results["Start"] = microtime();
                \Swoole\Runtime::enableCoroutine(true);
               Coroutine::create(function () use ($channel,&$results){
                   $results['A1'] = microtime();
                        $mysql=new \Swoole\Coroutine\MySQL();
                        $mysql->connect([
                            'host' => '127.0.0.1',
                            'port'=>'3306',
                            'user' => 'root',
                            'password' => 'gjm123',
                            'database' => 'laravel'
                        ]);
                   $data=$mysql->query("SELECT SLEEP(2)");
//                        $data=$mysql->query("SELECT * FROM test WHERE name=112312");

                       $mysql->close();
                       $channel->push($data);
                   });
                Coroutine::create(function () use ($channel,&$results){
                    $results['A2'] = microtime();
                    $mysql=new \Swoole\Coroutine\MySQL();
                    $mysql->connect([
                        'host' => '127.0.0.1',
                        'port'=>'3306',
                        'user' => 'root',
                        'password' => 'gjm123',
                        'database' => 'laravel'
                    ]);
//                    for ($i=0;$i<100;$i++){
//                        $mysql->query("INSERT test SET name='ABCDFER'");
//                    }
                    $data=$mysql->query("SELECT SLEEP(2)");
//                    $data=$mysql->query("SELECT * FROM test WHERE name='ABCDFER'");
                    $mysql->close();
                    $channel->push($data);
                });
                Coroutine::create(function () use ($channel,&$results){
                    $results['A3'] = microtime();
                    $mysql=new \Swoole\Coroutine\MySQL();
                    $mysql->connect([
                        'host' => '127.0.0.1',
                        'port'=>'3306',
                        'user' => 'root',
                        'password' => 'gjm123',
                        'database' => 'laravel'
                    ]);
//                    for ($i=0;$i<100;$i++){
//                        $mysql->query("INSERT test SET name='!@#$%^&%'");
//                    }
                    $data=$mysql->query("SELECT SLEEP(2)");
//                    $data=$mysql->query("SELECT * FROM test WHERE name='!@#$%^&%'");
                    $mysql->close();
                    $channel->push($data);
                });
                for($i=0;$i<3;$i++){
                    $results["data".$i]=$channel->pop();
                }
                $results["END"] = microtime();
                return $results;

        }
        function test3(){
            $o["Start"] = microtime();
            $results=$this->test();
            $o['data']=$results;
            $o["End"] = microtime();
            return json_encode($o);
        }
        function test1(){
            $o["Start"] = microtime();
            $results=[];
            DB::select("SELECT SLEEP(2)");
            DB::select("SELECT SLEEP(2)");
            DB::select("SELECT SLEEP(2)");
            $data1= DB::select("SELECT * FROM test WHERE name='!@#$%^&%'");
            $data2=DB::select("SELECT * FROM test WHERE name='ABCDFER'");
            $data3=DB::select("SELECT * FROM test WHERE name=112312");
            $results["data1"]=$data1;
            $results["data2"]=$data2;
            $results["data3"]=$data3;
            $o['data']=$results;
            $o["End"] = microtime();
            return json_encode($o);
        }

}